package com.ztesoft.sca.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.ztesoft.sca.model.aichat.AiChatDto;

@Component
public class AiChatAudioDaoImpl implements AiChatAudioDao{
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public AiChatAudioDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public List<AiChatDto> listAiChatAudio(long id, String type, int from, int size) throws Exception {
        String sqlStr="SELECT * FROM (SELECT ROWNUM rn,id,name,type,request_action,description,create_time "
        		+ "FROM (SELECT id,name,type,request_action,description,create_time FROM SCA_AUDIO_SOURCE)WHERE ROWNUM<=? ORDER BY id asc) t2 WHERE T2.rn >=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        if (from < 0) {
            from = Integer.MAX_VALUE; 
        }
        Object[] param = new Object[]{size,from};
        List<AiChatDto> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(AiChatDto.class));
        return list;
    }
    
    @Override
    public void insertAiChatAudio(AiChatDto aiChat) throws Exception {
        StringBuffer sqlBuf = new StringBuffer("insert into SCA_AUDIO_SOURCE" +
                "" +
                "  (ID, NAME,type,request_action,file_path,server_name,server_password,format,server_ip,sample_rate,description,create_time)" +
                "" +
                "VALUES" +
                "" +
                "  (?, ?,?, ?,?, ?,?, ?,?, ?,?, ?)");
        Object[] param = new Object[]{aiChat.getId(),aiChat.getName(),aiChat.getType(),aiChat.getRequestAction(),aiChat.getFilePath(),aiChat.getServerName(),
        		aiChat.getServerPassword(),aiChat.getFormat(),aiChat.getServerIp(),aiChat.getSampleRate(),aiChat.getDescription(),aiChat.getCreateTime()};
        this.jdbcTemplate.update(sqlBuf.toString(),param);
    }

	@Override
	public List<AiChatDto> listAiChatAudioConvert(long convert_id, String type, int from, int size) {
        String sqlStr="SELECT * FROM (SELECT ROWNUM rn,convert_id,convert_name,convert_class,convert_method,convert_extend_para,convert_description "
        		+ "FROM (SELECT convert_id,convert_name,convert_class,convert_method,convert_extend_para,convert_description"
        		+ " FROM SCA_AUDIO_CONVERT)WHERE ROWNUM<=? ORDER BY convert_id asc) t2 WHERE T2.rn >=?";
        StringBuffer sqlBuf =new StringBuffer(sqlStr);
        if (from < 0) {
            from = Integer.MAX_VALUE;
        }
        Object[] param = new Object[]{size,from};
        List<AiChatDto> list = this.jdbcTemplate.query(sqlBuf.toString(),param,BeanPropertyRowMapper.newInstance(AiChatDto.class));
        return list;
	}

	@Override
	public void insertAiChatAudioConvert(AiChatDto aiChat) throws Exception {
		// TODO Auto-generated method stub
        StringBuffer sqlBuf = new StringBuffer("insert into SCA_AUDIO_CONVERT" +
                "" +
                "  (convert_id,convert_name,convert_class,convert_method,convert_extend_para,convert_description,convert_create_time)" +
                "" +
                "VALUES" +
                "" +
                "  (?, ?,?, ?,?, ?,?)");
        Object[] param = new Object[]{aiChat.getConvertId(),aiChat.getConvertName(),aiChat.getConvertClass(),aiChat.getConvertMethod(),aiChat.getConvertExtendPara(),
        		aiChat.getConvertDescription(),aiChat.getconvertCreateTime()};
        this.jdbcTemplate.update(sqlBuf.toString(),param);	
	}
}
